#!/usr/bin/python
import pygst
pygst.require("0.10")
import gst
import pygtk
import gtk
import gtk.glade


__author__ = "lagaz"
__date__ = "$21/12/2008 19:15:59$"

if __name__ == "__main__":
    pass



class Main:
    def __init__(self):

        # Create gui bits and bobs

        self.wTree = gtk.glade.XML("equalizer.glade", "mainWindow")

        signals = {
            "on_play_clicked": self.onPlay,
            "on_stop_clicked": self.onStop,
            "on_quit_clicked": self.onQuit,
            "on_band0_changed": self.band0Changed,
            "on_band1_changed": self.band1Changed,
            "on_band2_changed": self.band2Changed,
            "on_band3_changed": self.band3Changed,
            "on_band4_changed": self.band4Changed,
            "on_band5_changed": self.band5Changed,
            "on_band6_changed": self.band6Changed,
            "on_band7_changed": self.band7Changed,
            "on_band8_changed": self.band8Changed,
            "on_band9_changed": self.band9Changed,

        }

        self.wTree.signal_autoconnect(signals)

        # Create GStreamer bits and bobs
        # gst-launch-0.10 filesrc location=lets_dance.mp3 ! mad ! audioconvert ! audioresample ! osssink

        # Gstreamer hal audio pipeline
        # gst-launch-0.10 -v halaudiosrc udi=/org/freedesktop/Hal/devices/pci_8086_27d8_alsa_capture_0 ! autoaudiosink

        # Listing hal devices: lshal
        # Searching hal devices
        # hal-find-by-property --key alsa.type --string playback

        self.pipeline = gst.Pipeline("mypipeline")

        self.filesrc = gst.element_factory_make("filesrc", "audio")
        self.filesrc.set_property("location", "lets_dance.mp3")

        self.hal_device = "/org/freedesktop/Hal/devices/pci_8086_27d8_sound_card_0_alsa_playback_0"

        #self.halsrc = gst.element_factory_make( "halaudiosrc" )
        #self.halsrc.set_property( "udi", self.hal_device )
        #self.halsrc = gst.element_factory_make( "alsasrc" )
        #self.halsrc.set_property( "device", "hw:0,0" )
        
        self.decoder = gst.element_factory_make("mad", "mp3-decoder")

        self.audioconvert = gst.element_factory_make("audioconvert")
        self.audioresample = gst.element_factory_make("audioresample")

        self.equalizer = gst.element_factory_make('equalizer-10bands')
        
        #self.sink = gst.element_factory_make("osssink", "sink")
        self.sink = gst.element_factory_make( "halaudiosink" )
        self.sink.set_property( "udi", self.hal_device )

        #self.pipeline.add( self.halsrc, self.equalizer, self.sink )
        self.pipeline.add( self.filesrc, self.decoder, self.audioconvert,
                           self.audioresample, self.equalizer, self.sink )
        
        #gst.element_link_many( self.halsrc, self.equalizer, self.sink )
        gst.element_link_many( self.filesrc, self.decoder, self.audioconvert,
                               self.audioresample, self.equalizer, self.sink )

        self.window = self.wTree.get_widget("mainWindow")
        self.window.show_all()

    def onPlay(self, widget):
        self.pipeline.set_state(gst.STATE_PLAYING)

    def onStop(self, widget):
        self.pipeline.set_state(gst.STATE_READY)

    def band0Changed(self, widget):
        self.bandNChanged( 0, widget.get_value() )
    def band1Changed(self, widget):
        self.bandNChanged( 1, widget.get_value() )
    def band2Changed(self, widget):
        self.bandNChanged( 2, widget.get_value() )
    def band3Changed(self, widget):
        self.bandNChanged( 3, widget.get_value() )
    def band4Changed(self, widget):
        self.bandNChanged( 4, widget.get_value() )
    def band5Changed(self, widget):
        self.bandNChanged( 5, widget.get_value() )
    def band6Changed(self, widget):
        self.bandNChanged( 6, widget.get_value() )
    def band7Changed(self, widget):
        self.bandNChanged( 7, widget.get_value() )
    def band8Changed(self, widget):
        self.bandNChanged( 8, widget.get_value() )
    def band9Changed(self, widget):
        self.bandNChanged( 9, widget.get_value() )

    def bandNChanged(self, bandNumber, value):
        self.equalizer.set_property( "band"+str(bandNumber), float( value ) )

    def onQuit(self, widget):
        gtk.main_quit()

start = Main()
gtk.main()
